package com.third.customer.model.dao.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.third.customer.model.dao.CustomerHistoryDao;
import com.third.customer.model.dao.param.CustomerHistoryParam;
import com.third.customer.model.mapper.CustomerHistoryMapper;
import com.third.customer.model.tables.CustomerHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created by hehuaichun on 2020/5/22.
 */
@Repository
public class CustomerHistoryDaoImpl implements CustomerHistoryDao {

    @Autowired
    private CustomerHistoryMapper customerHistoryMapper;

    @Override
    public Page<CustomerHistory> query(CustomerHistoryParam param) {
        Page<CustomerHistory> page = new Page<>(param.getPage(), param.getLimit());
        page.addOrder(OrderItem.desc("id"));
        return new LambdaQueryChainWrapper<>(customerHistoryMapper)
                .eq(CustomerHistory::getCustomerId, param.getCustomerId())
                .ge(param.getSinceTime() != null, CustomerHistory::getCreatedAt, param.getSinceTime())
                .le(param.getToTime() != null, CustomerHistory::getCreatedAt, param.getToTime())
                .page(page);
    }
}
